package com.lx.ngsl.api;

import java.util.Date;

import javax.security.auth.login.LoginException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.lx.ngsl.core.dto.Rap;
import com.lx.ngsl.core.dto.UserInfo;
import com.lx.ngsl.core.login.service.LoginAuth;
import com.lx.ngsl.core.user.entity.UserEntity;
import com.lx.ngsl.core.user.service.UserService;
import com.lx.ngsl.util.Md5Util;
import com.lx.ngsl.util.UuidUtil;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
/*
 * 基础接口类
 */
@RestController
@RequestMapping("/api/v0")
@Api(value="基础接口",protocols="http")
public class BaseApi {
	
	@Autowired
	private LoginAuth loginAuth;
	@Autowired
	private UserService userService;

	/*
	 * 登陆接口
	 */
	@PostMapping("/login")
	@ApiOperation("登录")
	public Rap login(
			@ApiParam(value = "用户名", name = "username", required = true)@RequestParam(value="username",required = true, defaultValue = "") String username,
			@ApiParam(value = "密码", name = "password", required = true)@RequestParam(value="password",required = true, defaultValue = "") String password,
			HttpSession session, HttpServletResponse response,HttpServletRequest request
			) {
		
		UserEntity loginUser = new UserEntity();
		loginUser.setUsername(username);
		loginUser.setPassword(password);
		//登录验证
		UserEntity user;
		try {
			user = loginAuth.loginAuth(loginUser);
		} catch (LoginException e) {
			e.printStackTrace();
			return Rap.error(e.getMessage());
		}
		
		//用户登录成功时生成一个唯一的token令牌
		String privatetoken = UuidUtil.id();
		
		UserInfo userInfo = userService.toUserInfo(user);
		//userInfo.setMwmm(password);//保存明文密码到模型对象中
		userInfo.setPrivatetoken(privatetoken);//保存私有令牌到模型对象中
		
		//将此用户的登录信息存储到服务器的session中。
		session.setAttribute("USER_CONTEXT", userInfo);
		
		
		return Rap.success("登陆成功！",userInfo);
	}
	
	@GetMapping("/test")
	@ApiOperation("测试")
	public Rap test() {
		return Rap.success("测试成功！");
	}
	
	/*
	 * 注册接口
	 */
	@PostMapping("/register")
	@ApiOperation("注册")
	public Rap register(
			@ApiParam(value = "用户名", name = "username", required = true)@RequestParam(value="username",required = true, defaultValue = "") String username,
			@ApiParam(value = "密码", name = "password", required = true)@RequestParam(value="password",required = true, defaultValue = "") String password
			) {
		if(userService.isUserExist(username)) {
			return Rap.error("用户名已存在");
		}
		UserEntity user = new UserEntity();
		user.setBuildDate(new Date());
		user.setAccountState(1);
		user.setUsername(username);
		user.setId(UuidUtil.id());
		user.setPassword(Md5Util.md5Str(password, "ngsl"));
		user.setRoleId("a609feca088648468a68c94939965ce2");
		userService.save(user);
		return Rap.success("注册成功");
	}
	
	
}
